{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'reg_lambda': [0.5, 1, 2], 'reg_alpha': [1.5, 2]}\n",
      "Best: -0.580341 using {'reg_alpha': 1.5, 'reg_lambda': 1}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\model_selection\\_search.py:761: DeprecationWarning: The grid_scores_ attribute was deprecated in version 0.18 in favor of the more elaborate cv_results_ attribute. The grid_scores_ attribute will not be available from 0.20\n",
      "  DeprecationWarning)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('mean_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split0_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split1_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split2_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split3_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('split4_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n",
      "e:\\Anaconda2\\lib\\site-packages\\sklearn\\utils\\deprecation.py:122: FutureWarning: You are accessing a training score ('std_train_score'), which will not be available by default any more in 0.21. If you need training scores, please set return_train_score=True\n",
      "  warnings.warn(*warn_args, **warn_kwargs)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\xe5\\xbd\\x93\\xe4\\xb8\\x8a\\xe9\\x9d\\xa2\\xe7\\x9a\\x84fit()\\xe6\\x96\\xb9\\xe6\\xb3\\x95\\xe8\\xbf\\x90\\xe8\\xa1\\x8c\\xe5\\xae\\x8c\\xe6\\xaf\\x95\\xef\\xbc\\x8c\\xe5\\xb9\\xb6\\xe5\\xb0\\x86\\xe6\\x95\\xb0\\xe6\\x8d\\xae\\xe5\\x86\\x99\\xe5\\x85\\xa5\\xe6\\x96\\x87\\xe4\\xbb\\xb6\\xe5\\x90\\x8e\\xef\\xbc\\x8c\\xe5\\x86\\x8d\\xe8\\xbf\\x90\\xe8\\xa1\\x8c\\xe6\\x9c\\xac\\xe6\\xae\\xb5\\xe4\\xbb\\xa3\\xe7\\xa0\\x81\\n#\\xe4\\xbb\\x8e\\xe6\\x96\\x87\\xe4\\xbb\\xb6\\xe4\\xb8\\xad\\xe8\\xaf\\xbb\\xe5\\x8f\\x96\\xe6\\x95\\xb0\\xe6\\x8d\\xae\\ncvresults = pd.read_csv(\"RLI_L1L2.csv\")\\nfparamgrid = open(\"RLI_L1L2.pickle\",\"rb\")\\nbestparam = pickle.load(fparamgrid)\\nfparamgrid.close()\\ntest_means =cvresults[ \\'mean_test_score\\' ]\\ntest_stds = cvresults [\\'std_test_score\\' ]\\ntrain_means = cvresults[ \\'mean_train_score\\' ]\\ntrain_stds = cvresults[ \\'std_train_score\\' ]\\n\\ntest_scores = np.array(test_means).reshape(len(reg_alpha), len(reg_lambda))\\ntrain_scores = np.array(train_means).reshape(len(reg_alpha), len(reg_lambda))\\n\\n    \\nfor i, value in enumerate(reg_alpha):\\n    pyplot.plot(reg_lambda, -test_scores[i], label= \\'reg_alpha:\\'   + str(value))\\n\\npyplot.legend()\\npyplot.xlabel( \\'reg_alpha\\' )                                                                                                      \\npyplot.ylabel( \\'-Log Loss\\' )\\n#pyplot.savefig( \\'reg_alpha_vs_reg_lambda1.png\\' )\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对L1\n",
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "import pickle\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "\n",
    "#读取数据\n",
    "dbpath = \"./data/\"\n",
    "train = pd.read_csv(dbpath +\"RentListingInquries_FE_train.csv\")\n",
    "\n",
    "y_train = train['interest_level']\n",
    "X_train = train.drop([\"interest_level\",\"Year\"],axis=1)\n",
    "                    \n",
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)\n",
    "\n",
    "reg_alpha = [ 1.5, 2]    #default = 0, 测试0.1,1，1.5，2\n",
    "reg_lambda = [0.5, 1, 2]      #default = 1，测试0.1， 0.5， 1，2\n",
    "\n",
    "param_test = dict(reg_alpha=reg_alpha, reg_lambda=reg_lambda)\n",
    "print(param_test)\n",
    "xgb = XGBClassifier(\n",
    "        learning_rate =0.1,\n",
    "        n_estimators=210,  #第二轮参数调整得到的n_estimators最优值\n",
    "        max_depth=6,#new param\n",
    "        min_child_weight=4,#new param\n",
    "        gamma=0,\n",
    "        subsample=0.8,#new param\n",
    "        colsample_bytree=0.9,#new param\n",
    "        colsample_bylevel = 0.7,\n",
    "        objective= 'multi:softprob',\n",
    "        seed=3)\n",
    "\n",
    "\n",
    "gsearch = GridSearchCV(xgb, param_grid = param_test, scoring='neg_log_loss',n_jobs=-1, cv=kfold)\n",
    "gsearch.fit(X_train , y_train)\n",
    "\n",
    "gsearch.grid_scores_, gsearch.best_params_,     gsearch.best_score_\n",
    "#将结果写入文件中\n",
    "print(\"Best: %f using %s\" % (gsearch.best_score_, gsearch.best_params_))\n",
    "#存入文件中\n",
    "pd.DataFrame(gsearch.cv_results_).to_csv(\"RLI_L1L2.csv\")\n",
    "fparamgrid = open(\"RLI_L1L2.pickle\",\"wb\")\n",
    "pickle.dump(gsearch.best_params_,fparamgrid,-1)\n",
    "fparamgrid.close()\n",
    "\n",
    "'''当上面的fit()方法运行完毕，并将数据写入文件后，再运行本段代码\n",
    "#从文件中读取数据\n",
    "cvresults = pd.read_csv(\"RLI_L1L2.csv\")\n",
    "fparamgrid = open(\"RLI_L1L2.pickle\",\"rb\")\n",
    "bestparam = pickle.load(fparamgrid)\n",
    "fparamgrid.close()\n",
    "test_means =cvresults[ 'mean_test_score' ]\n",
    "test_stds = cvresults ['std_test_score' ]\n",
    "train_means = cvresults[ 'mean_train_score' ]\n",
    "train_stds = cvresults[ 'std_train_score' ]\n",
    "\n",
    "test_scores = np.array(test_means).reshape(len(reg_alpha), len(reg_lambda))\n",
    "train_scores = np.array(train_means).reshape(len(reg_alpha), len(reg_lambda))\n",
    "\n",
    "    \n",
    "for i, value in enumerate(reg_alpha):\n",
    "    pyplot.plot(reg_lambda, -test_scores[i], label= 'reg_alpha:'   + str(value))\n",
    "\n",
    "pyplot.legend()\n",
    "pyplot.xlabel( 'reg_alpha' )                                                                                                      \n",
    "pyplot.ylabel( '-Log Loss' )\n",
    "#pyplot.savefig( 'reg_alpha_vs_reg_lambda1.png' )\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
